import datetime

from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect, JsonResponse
from .models import Attendance
from django.core.paginator import  Paginator
from .models import User
import time
import json
from django.db.models import Count
from django.http import HttpResponse
from pyecharts.charts import Bar, Gauge
from pyecharts import options as opts

def index_view(request):
    return render(request,"attendance/qiaodao.html")

def index_look(request):
    return render(request,'attendance/kaoqin_look.html')

#所有的上班签到数据
def all_clock(request):
    data = Attendance.objects.all()
    res=[]
    for attend in data:
        attend_data={}
        attend_data['id']=attend.user.id
        attend_data['name']=attend.user.user_name
        attend_data['sex']=attend.user.sex
        attend_data['dept']=attend.user.dept.title
        attend_data['check_time']=attend.check_time
        attend_data['check_state']="有效"
        res.append(attend_data)
        # 获取页面表单分页信息

    print(res)
    page = request.GET.get('page')
    limit = request.GET.get('limit')
    paginator = Paginator(res, limit)
    pages = paginator.page(page)
    work_report = [x for x in pages]
    reports = {"code": 0, "msg": "", "count": data.count(), "data": work_report}
    return JsonResponse(reports)

#所有的下班签退数据
def all_sign(request):
    data = Attendance.objects.all()
    res = []
    for attend in data:
        attend_data = {}
        attend_data['id'] = attend.user.id
        attend_data['name'] = attend.user.user_name
        attend_data['sex'] = attend.user.sex
        attend_data['dept'] = attend.user.dept.title
        attend_data['sign_back_time'] = attend.sign_back_time
        attend_data['sign_back_state'] = "有效"
        res.append(attend_data)
        # 获取页面表单分页信息

    print(res)
    page = request.GET.get('page')
    limit = request.GET.get('limit')
    paginator = Paginator(res, limit)
    pages = paginator.page(page)
    work_report = [x for x in pages]
    reports = {"code": 0, "msg": "", "count": data.count(), "data": work_report}
    return JsonResponse(reports)


def chock(request):
    print('**************************')
    print('测试')
    user=User.objects.get(id = request.session['userid'])
    new_time = datetime.datetime.now().strftime('%Y%m%d%H%M%S')

    print('**************************')
    print(user,">>>>>>>",new_time)
    Attendance.objects.create(user=user,check_state="有效")

    return JsonResponse({'code':200,'data':'签到成功'})


def sign(request):
    print('**************************')
    print('测试')
    user=User.objects.get(id = request.session['userid'])
    new_time = datetime.datetime.now()
    print('**************************')
    print(user,">>>>>>>",new_time)
    Attendance.objects.create(user=user,check_state="有效")

    return JsonResponse({'code':200,'data':'签退成功'})


def search(request):
        print('**************************')
        print('测试')
        user=User.objects.get(id = request.session['userid'])
        new_time = datetime.datetime.now()
        print('**************************')
        print(user,">>>>>>>",new_time)
        Attendance.objects.create(user=user,check_state="有效")

        return JsonResponse({'code':200,'data':'搜索成功'})


def all_seek(request):
    data = Attendance.objects.all()
    res = []
    for attend in data:
        attend_data = {}
        attend_data['id'] = attend.user.id
        attend_data['name'] = attend.user.user_name
        attend_data['sex'] = attend.user.sex
        attend_data['dept'] = attend.user.dept.title
        attend_data['check_time'] = attend.check_time
        attend_data['check_state'] = "有效"
        res.append(attend_data)
        # 获取页面表单分页信息

    print(res)
    page = request.GET.get('page')
    limit = request.GET.get('limit')
    paginator = Paginator(res, limit)
    pages = paginator.page(page)
    work_report = [x for x in pages]
    reports = {"code": 0, "msg": "", "count": data.count(), "data": work_report}
    return JsonResponse(reports)


def index_tongji(request):
    data = Attendance.objects.all()
    list_pro = []
    sign_obj = Attendance.objects.values('user_id').annotate(myCount=Count('check_state'))
    count=0
    # sign_obj = Attendance.objects.all('check_state'=='有效')
    # print(sign_obj)
    # 'SELECT * FROM `bookstore_book` WHERE `bookstore_book`.`market_price` > (`bookstore_book`.`price`)
    # count_pro = People_list.objects.aggregate(myCount=Count('place'))
    for i in sign_obj:
        # if i['check_state']=='有效':
        list_pro.append([i['user_id'], i['myCount']])
        count+=i['myCount']
    print(list_pro)
    print(count)
    print('%.2f'%(len(list_pro)/count*100))
    c = (
        Gauge()
            .add("", [("", '%.2f'%(len(list_pro)/count*100))])
            .set_global_opts(title_opts=opts.TitleOpts(title="内部考勤签到率"))
            .render(path="attendance/templates/attendance/gauge_base.html")
    )
    return render(request, 'attendance/gauge_base.html')
